import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-09-23
 * Time: 14:31
 */
//生产者消费者模型
public class Demo18 {
    public static void main(String[] args) {
        //建一个阻塞队列，作为交易场所
        BlockingDeque<Integer> queue=new LinkedBlockingDeque<>();

        //一个线程负责生产
        Thread t1=new Thread(() ->{
            int count=0;
           while(true){
               try {
                   queue.put(count);
                   System.out.println("生产的元素为："+count);
                   count++;
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });

        //一个线程负责消耗
        Thread t2=new Thread(() ->{
            while(true){
                try {
                    Integer n=queue.take();
                    System.out.println("消耗的元素为："+n);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
    }
}
